import Request from '@/plugins/luch-request/index.js'

const service = new Request({
	baseURL: "http://jie.fuliaoxx.com",
	timeout: 10000
})

function tips(message) {
	uni.showToast({
		icon: "none",
		title: message,
		duration: 2000
	});
}

// 请求拦截
service.interceptors.request.use((config) => {
	config.header = {
		...config.header,
		"token": uni.getStorageSync('token') ? uni.getStorageSync('token') : "",

	}
	return config
}, config => {
	return Promise.reject(config)
})

// 响应拦截
service.interceptors.response.use((res) => {

	if (res.statusCode == 200) {
		if (res.data.msg && res.data.msg != 'success' && res.data.msg != '获取成功' && res.data.msg != '获取成功！' &&
			res.data.msg != '操作成功' && res
			.data.msg != '创建成功' && res
			.data.msg != '获取到收货地址'&& res.data.msg != '修改成功'&& res.data.msg != '恭喜中奖') {
			uni.showToast({
				icon: 'none',
				title: res.data.msg,
				duration: 1000
			})
		}
		if (res.data.code == 0) {
			uni.showToast({
				icon: 'none',
				title: res.data.msg,
				duration: 1000
			})
		}
		return res.data;
	} else {}
}, error => {
	//获取状态码
	if (error.statusCode == 401) {
		uni.removeStorageSync('token')
		uni.removeStorageSync('userInfo')
		uni.reLaunch({
			url: "/pages/index/login2"
		})
	}
	const status = error.data.code || error.statusCode || undefined;
	const message = error.data.msg || error.errMsg || '未知错误';
});

export default service;